load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("//bazel:defs.bzl", "rust_bench")

rust_library(
    name = "adapter",
    srcs = glob(
        ["src/**"],
    ),
    crate_name = "ic_btc_adapter",
    visibility = [
        "//rs/bitcoin/adapter/test_utils:__subpackages__",
        "//rs/pocket_ic_server:__subpackages__",
    ],
    deps = [
        # Keep sorted.
        "//rs/bitcoin/service",
        "//rs/bitcoin/validation",
        "//rs/config",
        "//rs/http_endpoints/async_utils",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "@crate_index//:bitcoin",
        "@crate_index//:futures",
        "@crate_index//:hashlink",
        "@crate_index//:hex",
        "@crate_index//:http",
        "@crate_index//:lmdb_rkv",
        "@crate_index//:parking_lot",
        "@crate_index//:prometheus",
        "@crate_index//:prost",
        "@crate_index//:rand",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:slog-async",
        "@crate_index//:static_assertions",
        "@crate_index//:thiserror",
        "@crate_index//:tokio",
        "@crate_index//:tokio-socks",
        "@crate_index//:tonic",
        "@crate_index//:tower",
    ],
)

rust_binary(
    name = "ic-btc-adapter",
    srcs = glob(
        ["bin/ic-btc-adapter/**"],
    ),
    visibility = ["//rs:release-pkg"],
    deps = [
        # Keep sorted.
        ":adapter",
        "//rs/bitcoin/service",
        "//rs/bitcoin/validation",
        "//rs/config",
        "//rs/http_endpoints/async_utils",
        "//rs/monitoring/adapter_metrics/server",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "@crate_index//:bitcoin",
        "@crate_index//:clap",
        "@crate_index//:futures",
        "@crate_index//:hashlink",
        "@crate_index//:hex",
        "@crate_index//:http",
        "@crate_index//:parking_lot",
        "@crate_index//:prometheus",
        "@crate_index//:prost",
        "@crate_index//:rand",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:slog-async",
        "@crate_index//:static_assertions",
        "@crate_index//:thiserror",
        "@crate_index//:tokio",
        "@crate_index//:tokio-socks",
        "@crate_index//:tonic",
        "@crate_index//:tower",
    ],
)

# not a rust_test because it's meant to be run manually.
rust_binary(
    name = "adapter-stress-test",
    srcs = ["bin/adapter-stress-test.rs"],
    deps = [
        ":adapter",
        "//rs/bitcoin/service",
        "@crate_index//:bitcoin",
        "@crate_index//:clap",
        "@crate_index//:hyper-util",
        "@crate_index//:tokio",
        "@crate_index//:tonic",
        "@crate_index//:tower",
    ],
)

rust_test(
    name = "adapter_test",
    compile_data = [
        "test_data/first_2500_mainnet_headers.json",
        "test_data/first_2500_testnet_headers.json",
    ],
    crate = ":adapter",
    tags = ["requires-network"],
    deps = [
        # Keep sorted.
        "//rs/bitcoin/adapter/test_utils",
        "//rs/bitcoin/replica_types",
        "//rs/test_utilities/logger",
        "@crate_index//:tempfile",
    ],
)

rust_test(
    name = "adapter_integration_test",
    timeout = "long",
    srcs = ["tests/integration.rs"],
    data = [
        # Keep sorted.
        "//:bitcoind",
        "//:dogecoind",
        "@bitcoin_adapter_mainnet_blocks//file",
        "@bitcoin_adapter_mainnet_headers//file",
        "@bitcoin_adapter_testnet_blocks//file",
        "@bitcoin_adapter_testnet_headers//file",
    ],
    env = {
        "BITCOIN_CORE_PATH": "$(rootpath //:bitcoind)",
        "DOGECOIN_CORE_PATH": "$(rootpath //:dogecoind)",
        "HEADERS_DATA_PATH": "$(rootpath @bitcoin_adapter_mainnet_headers//file)",
        "BLOCKS_DATA_PATH": "$(rootpath @bitcoin_adapter_mainnet_blocks//file)",
        "TESTNET_HEADERS_DATA_PATH": "$(rootpath @bitcoin_adapter_testnet_headers//file)",
        "TESTNET_BLOCKS_DATA_PATH": "$(rootpath @bitcoin_adapter_testnet_blocks//file)",
        "RUST_TEST_THREADS": "4",
        "RUST_TEST_NOCAPTURE": "1",
    },
    flaky = True,
    tags = [
        "cpu:4",
    ],
    # Because dogecoind binary package is not available on darwin or arm64.
    target_compatible_with = [
        "@platforms//cpu:x86_64",
        "@platforms//os:linux",
    ],
    deps = [
        ":adapter",
        "//rs/bitcoin/adapter/test_utils",
        "//rs/bitcoin/client",
        "//rs/bitcoin/replica_types",
        "//rs/config",
        "//rs/interfaces/adapter_client",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "@crate_index//:bitcoin",
        "@crate_index//:tempfile",
        "@crate_index//:tokio",
    ],
)

rust_bench(
    name = "e2e_bench",
    testonly = True,
    srcs = ["benches/e2e.rs"],
    data = [
        # Keep sorted.
        "@bitcoin_adapter_mainnet_headers//file",
        "@doge_headers_800k_mainnet_auxpow//file",
    ],
    env = {
        "BITCOIN_MAINNET_HEADERS_DATA_PATH": "$(rootpath @bitcoin_adapter_mainnet_headers//file)",
        "DOGECOIN_MAINNET_HEADERS_DATA_PATH": "$(rootpath @doge_headers_800k_mainnet_auxpow//file)",
    },
    deps = [
        # Keep sorted.
        ":adapter",
        "//rs/bitcoin/adapter/test_utils",
        "//rs/bitcoin/client",
        "//rs/bitcoin/replica_types",
        "//rs/bitcoin/service",
        "//rs/bitcoin/validation",
        "//rs/config",
        "//rs/crypto/test_utils/reproducible_rng",
        "//rs/http_endpoints/async_utils",
        "//rs/interfaces/adapter_client",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "//rs/test_utilities/logger",
        "@crate_index//:bitcoin",
        "@crate_index//:criterion",
        "@crate_index//:flate2",
        "@crate_index//:futures",
        "@crate_index//:hashlink",
        "@crate_index//:hex",
        "@crate_index//:http",
        "@crate_index//:ic-btc-interface",
        "@crate_index//:mockall",
        "@crate_index//:parking_lot",
        "@crate_index//:prometheus",
        "@crate_index//:prost",
        "@crate_index//:rand",
        "@crate_index//:scrypt",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:sha2",
        "@crate_index//:slog",
        "@crate_index//:slog-async",
        "@crate_index//:static_assertions",
        "@crate_index//:tempfile",
        "@crate_index//:thiserror",
        "@crate_index//:tokio",
        "@crate_index//:tokio-socks",
        "@crate_index//:tonic",
        "@crate_index//:tower",
    ],
)
